package com.winchain.chainbill.asset.biz.credit.rx;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.stellar.StellarConfiguration;
import com.winchain.chainbill.asset.biz.credit.AssetEvent;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.trade.service.TradeService;

import tk.mybatis.mapper.util.StringUtil;

@Component
public class CreditAssetTranForInitSubscriber extends TxSubscriber<AssetEvent<AcctTxRec>> {
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;
	@Autowired
	private BillAcctService billAcctService;

	public void onApplicationEvent(AssetEvent<AcctTxRec> t) {
		String txSn = SystemUtil.getUUID();
		List<AcctTxRec> records = t.getRecords();

		List<AcctTxRec> list = new ArrayList();

		String finId = t.getStringValue("finId");
		String grpId = t.getStringValue("grpId");
		AcctTxRec record;
		for (Iterator localIterator = records.iterator(); localIterator.hasNext();) {
			record = (AcctTxRec) localIterator.next();
			AcctTxRec tranCredit = getAcctTxRec(record, grpId, finId);

			tranCredit.setTxSn(txSn);
			list.add(tranCredit);
		}
		Object result = this.tradeService.fire((AcctTxRec) list.get(0));
		for (AcctTxRec txRec : list) {
			txRec.setTxSnHost((String) ((Map) result).get("tx_sn"));
			t.setValue("txSnHost", txRec.getTxSnHost());
			if ("S".equals(((Map) result).get("ret_code"))) {
				this.tradeService.callback(txRec);
				setCreditCode(txRec);
			} else {
				this.tradeService.rollback(txRec);
			}
		}
		if (StringUtil.isEmpty(finId)) {
			finId = CommonDataUtil.getCurEntId();
		}
		this.billAcctService.openChainTransitAcct(finId);
	}

	private AcctTxRec getAcctTxRec(AcctTxRec txRec, String grpId, String finId) {
		if (StringUtil.isEmpty(finId)) {
			finId = CommonDataUtil.getCurEntId();
		}
		BigDecimal billAmt = txRec.getTxAmt();
		String cusId = txRec.getBizId();
		String abs = "����������";
		String txAcctType = "1";
		String cpAcctType = "1";

		String bizType = txRec.getTxType();

		txRec.setTxId(SystemUtil.getUUID());
		txRec.setCurrCode(txRec.getCurrCode());
		txRec.setTxChnCode("C");

		txRec.setTxAcctNo(StellarConfiguration.getCentralbankAddr());
		txRec.setTxPassword(StellarConfiguration.getCentralbankSeed());
		txRec.setTxBkpId("COLD_WALLET");

		txRec.setTxAmt(billAmt);
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs(abs);
		txRec.setTxType(bizType);

		txRec.setTxSts("I");

		AcctBas acct = getAcctBas(cusId, cpAcctType, null);
		txRec.setCpAcctNo(acct.getAcctNo());
		txRec.setCpAcctNm(acct.getAcctNm());
		txRec.setCpBankNm(acct.getFiNm());

		String conBizType = "40";
		if (AssetCode.XTB.name().equals(txRec.getCurrCode())) {
			conBizType = "41";
		} else if (AssetCode.FQT.name().equals(txRec.getCurrCode())) {
			conBizType = "42";
		}
		AcctBkp cpAcctBkp = this.billAcctBlcService.getCreditAcctBkpByCont(finId, cusId, txRec.getCurrCode(),
				conBizType);
		if (cpAcctBkp == null) {
			cpAcctBkp = this.billAcctBlcService.initAcctBkp(acct.getAcctNo(), txRec.getCurrCode(), grpId, null,
					BigDecimal.ZERO);
		}
		txRec.setCpBkpId(cpAcctBkp.getBkpId());

		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);

		txRec.setDataScLvl("B");

		return txRec;
	}

	private void setCreditCode(AcctTxRec tx) {
		String optType = tx.getOptType();
		String txType = tx.getTxType();
		if (("qt_inc".equals(optType)) && ("qt_credit".equals(txType))) {
			String cpBkpId = tx.getCpBkpId();
			String finId = CommonDataUtil.getCurEntId();
			String cusId = tx.getBizId();
			String bizType = "40";
			if (AssetCode.XTB.name().equals(tx.getCurrCode())) {
				bizType = "41";
			} else if (AssetCode.FQT.name().equals(tx.getCurrCode())) {
				bizType = "42";
			}
			this.billAcctBlcService.setCreditCode(finId, cusId, cpBkpId, bizType, tx.getCurrCode());
		}
	}
}
